package iceberg.violations;

import iceberg.Reference;
import iceberg.constraints.IConstraintResult;
import iceberg.constraints.WrongLayerForFunctionalityResult;
import iceberg.domain.Functionality;
import iceberg.domain.Layer;
import iceberg.domain.Rule;
import iceberg.domain.StructureDefinition;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class FormattingRule implements Rule {
	private static final Set<String> formatters = new HashSet<String>(
			Arrays.asList("java.text.DateFormat", "java.text.SimpleDateFormat", "java.text.MessageFormat", "java.text.NumberFormat")
	);

	private final StructureDefinition structureDefinition;

	public FormattingRule(StructureDefinition structureDefinition) {
		this.structureDefinition = structureDefinition;
	}

	@Override
	public IConstraintResult performCheck(Reference reference) {
		String name = reference.getTo().getName();
		boolean contains = formatters.contains(name);
		if (contains) {
			Layer layer = structureDefinition.getWorkableLayer(reference.getTo());
			if (layer != null && layer.getFunctionality() == Functionality.DOMAIN) {
				return new WrongLayerForFunctionalityResult(new FormatterViolationType());
			}
		}

		return null;
	}
}
